5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.3.0
47 require_once 'PHPUnit/Util/Filter.php';
48 require_once 'PHPUnit/Util/Filesystem.php';
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53 * Utility methods to load PHP sourcefiles.
57 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
59 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
60 * @version Release: @package_version@
61 * @link http://www.phpunit.de/
62 * @since Class available since Release 2.3.0
64 class PHPUnit_Util_Fileloader
67 * Path to the PHP interpreter that is to be used.
69 * @var string $phpBinary
71 public static $phpBinary = NULL;
74 * Checks if a PHP sourcefile is readable and is optionally checked for
75 * syntax errors through the syntaxCheck() method. The sourcefile is
76 * loaded through the load() method.
78 * @param string $filename
79 * @param boolean $syntaxCheck
80 * @throws RuntimeException
82 public static function checkAndLoad($filename, $syntaxCheck = TRUE)
84 if (!is_readable($filename)) {
85 $filename = './' . $filename;
88 if (!is_readable($filename)) {
89 throw new RuntimeException(
91 'File "%s" does not exist.',
92 str_replace('./', '', $filename)
98 self::syntaxCheck($filename);
101 self::load($filename);
105 * Loads a PHP sourcefile.
107 * @param string $filename
109 * @since Method available since Release 3.0.0
111 public static function load($filename)
113 $filename = PHPUnit_Util_Filesystem::fileExistsInIncludePath($filename);
116 throw new RuntimeException(
118 'File "%s" does not exist.',
124 $oldVariableNames = array_keys(get_defined_vars());
126 include_once $filename;
128 $newVariables = get_defined_vars();
129 $newVariableNames = array_diff(array_keys($newVariables), $oldVariableNames);
131 foreach ($newVariableNames as $variableName) {
132 if ($variableName != 'oldVariableNames') {
133 $GLOBALS[$variableName] = $newVariables[$variableName];
141 * Uses a separate process to perform a syntax check on a PHP sourcefile.
143 * PHPUnit_Util_Fileloader::$phpBinary contains the path to the PHP
144 * interpreter used for this. If unset, the following assumptions will
147 * 1. When the PHP CLI/CGI binary configured with the PEAR Installer
148 * (php_bin configuration value) is readable, it will be used.
150 * 2. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
151 * variable does not contain the string "PHPUnit", $_SERVER['_']
152 * is assumed to contain the path to the current PHP interpreter
153 * and that will be used.
155 * 3. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
156 * variable contains the string "PHPUnit", the file that $_SERVER['_']
157 * points is assumed to be the PHPUnit TextUI CLI wrapper script
158 * "phpunit" and the binary set up using #! on that file's first
159 * line of code is assumed to contain the path to the current PHP
160 * interpreter and that will be used.
162 * 4. The current PHP interpreter is assumed to be in the $PATH and
163 * to be invokable through "php".
165 * @param string $filename
166 * @throws RuntimeException
167 * @since Method available since Release 3.0.0
169 protected static function syntaxCheck($filename)
171 if (self::$phpBinary === NULL) {
172 if (is_readable('@php_bin@')) {
173 self::$phpBinary = '@php_bin@';
176 else if (PHP_SAPI == 'cli' && isset($_SERVER['_']) &&
177 strpos($_SERVER['_'], 'phpunit') !== FALSE) {
178 $file = file($_SERVER['_']);
179 $tmp = explode(' ', $file[0]);
180 self::$phpBinary = trim($tmp[1]);
183 if (!is_readable(self::$phpBinary)) {
184 self::$phpBinary = 'php';
186 self::$phpBinary = escapeshellarg(self::$phpBinary);
190 $command = self::$phpBinary . ' -l ' . escapeshellarg($filename);
192 if (DIRECTORY_SEPARATOR == '\\') {
193 $command = '"' . $command . '"';
196 $output = shell_exec($command);
198 if (strpos($output, 'Errors parsing') !== FALSE) {
199 throw new RuntimeException($output);